package com.atguigu.gmall.web.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.list.model.SearchAttr;
import com.atguigu.gmall.list.model.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品列表接口
 * </p>
 *
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 门户页面中商品检索页面渲染
     *
     * @param searchParam
     * @param model
     * @return
     */
    @GetMapping("/list.html")
    public String searchHtml(SearchParam searchParam, Model model) {

        //远程调用搜索微服务查询业务数据 以及过滤数据 回显业务所需数据
        //1.进行站内搜索搜索显然业务数据,过滤项数据
        Result<Map> result = listFeignClient.search(searchParam);
        model.addAllAttributes(result.getData());
        //2.为了显然搜索页面除了以上feign接口响应数据外还需要四项数据
        //2.1封装key:searchParam Val :用户提交搜索条件
        model.addAttribute("searchParam", searchParam);
        //2.2 封装回显页面地址栏请求参数数据 key:urlParam val :用户提交参数完成url地址"lisy.jhtml"
        //keyword=手机&props="
        model.addAttribute("urlParam", this.makeUrlParam(searchParam));
        //2.3封装会页面选中平台属性效果数据
        model.addAttribute("propsParamList", this.makePropsParamList(searchParam));
        //2.4封装回显排序信息数据 key :orderMap val:排序信息 包含两个属性1type-1综合 2.价格 2sort-asc/desc
        model.addAttribute("orderMap", this.makeOrderMap(searchParam.getOrder()));
        //2.5封装品牌数据 key:tradmarkparam val选中品牌
        model.addAttribute("trademarkParam", this.makeTrademarkParam(searchParam));
        return "/list/index";
    }

    /**
     * //2.4封装回显排序信息数据 key :orderMap val:排序信息 包含两个属性1type-1综合 2.价格 2sort-asc/desc
     * @param order
     * @return
     */
    private Map makeOrderMap(String order) {

        HashMap<String, String> orderMap = new HashMap<>();
        if (StringUtils.isNotBlank(order)){
            String[] split = order.split(":");
            orderMap.put("type",split[0]);
            orderMap.put("sort",split[1]);
        }else{
            //没有选择排序
            orderMap.put("type","3");
            orderMap.put("sort","asc");
        }
        return orderMap;

    }

    /**
     * 2.5封装品牌数据 key:tradmarkparam val选中品牌
     *
     * @param searchParam
     * @return
     */
    private Object makeTrademarkParam(SearchParam searchParam) {
        if (StringUtils.isNotBlank(searchParam.getTrademark())) {
            String[] split = searchParam.getTrademark().split(":");
            if (split != null && split.length == 2) {
                return "品牌:" + split[1];
            }
        }
        return null;
    }

    /**
     * 根据用户提交选中平台属性过滤条件,返回用户已选择平台属性过滤条件
     *
     * @param searchParam
     * @return
     */
    private List<SearchAttr> makePropsParamList(SearchParam searchParam) {
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0) {
            List<String> propList = Arrays.asList(props);
            List<SearchAttr> searchAttrList = propList.stream().map(attrInfoStr -> {
                String[] split = attrInfoStr.split(":");
                if (split != null && split.length == 3) {
                    SearchAttr searchAttr = new SearchAttr();
                    searchAttr.setAttrId(Long.valueOf(split[0]));
                    searchAttr.setAttrValue(split[1]);
                    searchAttr.setAttrName(split[2]);
                    return searchAttr;
                }
                return null;
            }).collect(Collectors.toList());
            return searchAttrList;
        }
        return null;

    }

    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder stringBuilder = new StringBuilder("/list.html?");
        if (StringUtils.isNotBlank(searchParam.getKeyword())) {
            stringBuilder.append("&keyword=" + searchParam.getKeyword());
        }
        if (StringUtils.isNotBlank(searchParam.getTrademark())) {
            stringBuilder.append("&trademark=" + searchParam.getTrademark());
        }
        if (searchParam.getCategory1Id() != null) {
            stringBuilder.append("&category1Id=" + searchParam.getCategory1Id());
        }
        if (searchParam.getCategory2Id() != null) {
            stringBuilder.append("&category2Id=" + searchParam.getCategory2Id());
        }
        if (searchParam.getCategory3Id() != null) {
            stringBuilder.append("&category3Id=" + searchParam.getCategory3Id());
        }
        //将用户选中平台属性数组遍历拼接到url地址栏中
        if (searchParam.getProps() != null && searchParam.getProps().length > 0) {
            for (String prop : searchParam.getProps()) {

                String[] split = prop.split(":");
                if (split != null && split.length == 3) {
                    stringBuilder.append("&props").append(prop);
                }
            }
        }
        return stringBuilder.toString();
    }
}

